package com.pj.compose.ui.compose

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.pj.compose.ui.theme.c666666

@Composable
fun BottomListSheetContent(
    modifier: Modifier = Modifier,
    itemModifier: Modifier = Modifier,
    title: String = "",
    items: List<String>,
    painters: List<Painter> = emptyList(),
    showMark: Boolean = false,
    markIndex: Int = 0,
    horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
    cancelText: String = "取消",
    cancelColor:Color = MaterialTheme.colorScheme.primary,
    iconContentColor: Color = AlertDialogDefaults.iconContentColor,
    titleContentColor: Color = AlertDialogDefaults.titleContentColor,
    textContentColor: Color = AlertDialogDefaults.textContentColor,
    onCancel: (() -> Unit)? = null,
    onItemClick: ((Int) -> Unit)? = null
) {
    val maxListHeight = LocalConfiguration.current.screenHeightDp * 0.75
    Column(
        modifier = modifier.fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        if (title.isNotEmpty()) {
            Text(text = title, modifier = Modifier.padding(10.dp), color = titleContentColor)
            Divider()
        }
        LazyColumn(Modifier.sizeIn(minHeight = 50.dp, maxHeight = maxListHeight.dp)) {
            items.forEachIndexed { index, s ->
                item {
                    SheetTextItem(
                        text = s,
                        modifier = itemModifier,
                        icon = {
                            if (painters.isNotEmpty()) {
                                Image(
                                    painter = painters[index % painters.size],
                                    contentDescription = "Icon",
                                    modifier = Modifier.padding(end = 10.dp),
                                    colorFilter = ColorFilter.tint(iconContentColor)
                                )
                            }
                        },
                        textColor = textContentColor,
                        showMark = (showMark && index == markIndex),
                        horizontalArrangement = horizontalArrangement
                    ) {
                        onItemClick?.invoke(index)
                    }
                }
            }
        }
        onCancel?.also {
            TextButton(onClick = it) {
                Text(text = cancelText, color = cancelColor, fontSize = 16.sp)
            }
        }


    }
}


@Composable
fun BottomGridContent(
    modifier: Modifier = Modifier,
    title: String = "",
    items: List<String>,
    painters: List<Painter> = emptyList(),
    columns: Int = 4,
    cancelText: String = "取消",
    onCancel: (() -> Unit)? = null,
    onItemClick: ((Int) -> Unit)? = null
) {
//    val maxGridHeight = LocalConfiguration.current.screenHeightDp * 0.6
    Column(modifier = modifier) {
        if (title.isNotEmpty()) {
            Text(
                text = title, modifier = Modifier
                    .padding(10.dp)
                    .fillMaxWidth(),
                fontSize = 16.sp,
                textAlign = TextAlign.Center
            )
            Divider()
        }
        LazyVerticalGrid(columns = GridCells.Fixed(columns)) {
            items.forEachIndexed { index, s ->
                item {
                    Column(
                        modifier = Modifier
                            .clickable { onItemClick?.invoke(index) }
                            .padding(vertical = 15.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        if (painters.isNotEmpty()) {
                            Image(
                                painter = painters[index % painters.size],
                                contentDescription = "Image"
                            )
                        }
                        Text(
                            text = s,
                            modifier = Modifier.padding(top = 5.dp),
                            color = c666666,
                            fontSize = 12.sp,
                        )
                    }
                }
            }
        }
        onCancel?.also {
            Divider()
            TextButton(onClick = it, modifier = Modifier.fillMaxWidth()) {
                Text(text = cancelText)
            }
        }
    }
}

@Composable
private fun SheetTextItem(
    text: String,
    modifier: Modifier = Modifier,
    icon: @Composable (() -> Unit) = {},
    textColor:Color = Color.Black,
    showMark: Boolean = false,
    horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
    onClick: (() -> Unit)? = null
) {
    Column(
        modifier = modifier
            .fillMaxWidth()
    ) {
        Row(
            modifier = modifier
                .fillMaxWidth()
                .padding(15.dp)
                .clickable { onClick?.invoke() },
            horizontalArrangement = horizontalArrangement
        ) {
            icon()
            Text(text = text, color = textColor)
            if (showMark) {
                Row(
                    modifier = Modifier
                        .padding(end = 10.dp)
                        .weight(1f),
                    horizontalArrangement = Arrangement.End
                ) {
                    Icon(
                        imageVector = Icons.Default.Check,
                        contentDescription = "mark",
                        tint = MaterialTheme.colorScheme.primary
                    )

                }
            }
        }
        Divider()
    }

}

